<?php
/**
 * @user:andy
 * @date:2021/7/3
 * @company:http://www.sjdphp.com
 * @desc: 管理员类
 */

namespace app\admin\controller;


use app\common\controller\BackBaseController;
use app\common\model\Role as RoleModel;
use app\common\server\Json;
use app\common\server\Tools;

class Back extends BackBaseController
{
    protected $model_id = 'back_id';
    protected $model_name = 'back_name';
    protected $model_status = 'back_status';
    protected $model_cate = 'role_id';


    public function add(){
        if($this->request->isPost()){
            ## 接受参数
            $param = $this->request->param();
            ## 校验参数
            $validate = self::getBusExample($this->request->controller(),2);
            if(!$validate->scene("add")->check($param)) return Json::error($validate->getError());

            ## 默认状态
            $param["back_status"] = config("project.mysql_code.table_pending");
            ## 操作人
            $param["operator"] = $this->request->BackInfo["back_name"];

            ## 获取角色名称
            $roleInfo = RoleModel::where(array("role_id" => $param["role_id"]))->find();
            if(!empty($roleInfo)){
                $param["role_name"] = $roleInfo["role_name"];
            }

            ## 生成密码密钥
            $param["back_key"] = Tools::getRandStr(32);

            ## 加密密码
            $param["password"] = Tools::md5AdminPwd($param["password"],$param["back_key"]);

            ## 调用业务层处理
            $class = self::getBusExample($this->request->controller());
            $res = $class::insertOrUpdateAndDel($param);

            return $res;
        }else{
            ## 获取角色列表
            $roles = RoleModel::where(array(['role_id','<>',1],['role_status','<>',config("project.mysql_code.table_delete")]))->field('role_id,role_name')->select();
            return view('',[
                'roles' => empty($roles) ? $roles : $roles->toArray()
            ]);
        }
    }


    public function edit(){
        if($this->request->isPost()){
            ## 接受参数
            $param = $this->request->param();
            ## 校验参数
            $validate = self::getBusExample($this->request->controller(),2);
            if(!$validate->scene("edit")->check($param)) return Json::error($validate->getError());

            ## 操作人
            $param["operator"] = $this->request->BackInfo["back_name"];

            ## 获取角色名称
            $roleInfo = RoleModel::where(array("role_id" => $param["role_id"]))->find();
            if(!empty($roleInfo)){
                $param["role_name"] = $roleInfo["role_name"];
            }

            ## 判断密码长度
            if(strlen($param["password"]) > 32 || strlen($param["password"]) < 32){
                ## 生成密码密钥
                $param["back_key"] = Tools::getRandStr(32);

                ## 加密密码
                $param["password"] = Tools::md5AdminPwd($param["password"],$param["back_key"]);
            }


            ## 调用业务层处理
            $class = self::getBusExample($this->request->controller());
            $res = $class::insertOrUpdateAndDel($param,2);

            return $res;
        }else{
            ## 获取修改的ID
            $back_id = $this->request->param("back_id",0,'intval');
            if(!empty($back_id)){
                $class = self::getBusExample($this->request->controller());
                $info = $class::getInfo(array("back_id" => $back_id));
            }
            ## 获取角色列表
            $roles = RoleModel::where(array(['role_id','<>',1],['role_status','<>',config("project.mysql_code.table_delete")]))->field('role_id,role_name')->select();
            return view('',[
                'roles' => empty($roles) ? $roles : $roles->toArray(),
                'info'  => $info
            ]);
        }
    }
}